package disruptor;

import netty.util.StartAndStop;
import test.PerformanceAnalyze;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class NormalQueue extends StartAndStop {
    ArrayBlockingQueue<Long> queue = new ArrayBlockingQueue<>(64);

    Executor executor = Executors.newFixedThreadPool(1);

    PerformanceAnalyze performanceAnalyze = new PerformanceAnalyze();

    public void start() {
        List<Long> longList = new ArrayList<>(100);

        executor.execute(() -> {
            try {
                while (running) {
                    Long e = queue.take();
                    longList.add(e);
                    queue.drainTo(longList, 99);

                    longList.forEach(time -> {
                        performanceAnalyze.delay(System.nanoTime() - time);
                        performanceAnalyze.meter();
                    });
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        while (running) {
            queue.offer(System.nanoTime());
        }
    }

    public static void main(String[] args) {
        new NormalQueue().start();

    }
}
